package JPAController;

import Game.Deck;
import JPAController.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;

public class DeckJpaController implements Serializable {

    public DeckJpaController() {
        this.emf = Persistence.createEntityManagerFactory("CardGamePU");
    }
    
    public DeckJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Deck deck) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(deck);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Deck deck) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            deck = em.merge(deck);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = deck.getName();
                if (findDeck(id) == null) {
                    throw new NonexistentEntityException("The deck with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Deck deck;
            try {
                deck = em.getReference(Deck.class, id);
                deck.getName();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The deck with id " + id + " no longer exists.", enfe);
            }
            em.remove(deck);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Deck> findDeckEntities() {
        return findDeckEntities(true, -1, -1);
    }

    public List<Deck> findDeckEntities(int maxResults, int firstResult) {
        return findDeckEntities(false, maxResults, firstResult);
    }

    private List<Deck> findDeckEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Deck as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Deck findDeck(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Deck.class, id);
        } finally {
            em.close();
        }
    }

    public int getDeckCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Deck as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
